package com.example.statistics.ui.inventory

import android.os.Bundle
import android.view.View
import android.widget.TextView
import com.example.statistics.R
import com.example.statistics.bean.InventoryOrder
import com.example.statistics.common.BaseLoadingListActivity
import com.example.statistics.ex.*
import com.example.statistics.net.UrlConfig
import com.example.statistics.ui.bank.Config.toStatementActivity
import com.example.statistics.utils.Constants
import com.example.statistics.utils.DefaultConfig
import com.example.statistics.utils.activityForResult
import com.jhj.slimadapter.SlimAdapter
import com.jhj.slimadapter.holder.ViewInjector
import org.jetbrains.anko.toast

class InventorySubListActivity : BaseLoadingListActivity<InventoryOrder>() {

    override val title: String
        get() = "销售列表"
    override val url: String
        get() = UrlConfig.INVENTORY_SALES_LIST
    override val itemLayoutRes: Int
        get() = R.layout.list_item_inventory_sub

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        topBarRightImage() {
            activityForResult<InventorySubActivity>() {
                refresh()
            }
        }
    }

    override fun itemViewConvert(adapter: SlimAdapter, injector: ViewInjector, bean: InventoryOrder, position: Int) {
        injector
            .with<TextView>(R.id.tv_status) {
                if (bean.statementList.isNullOrEmpty()) {
                    it.visibility = View.GONE
                } else {
                    it.visibility = View.VISIBLE
                }
            }
            .text(R.id.tv_title, bean.customerName)
            .text(R.id.tv_date, bean.date.goTimeFormat())
            .with<TextView>(R.id.tv_content) {
                val stringBuilder = StringBuilder()
                bean.salesProductList?.forEachIndexed { index, product ->
                    if (index != 0) stringBuilder.append("\n")
                    stringBuilder.append(product.factoryNo)
                    stringBuilder.append(product.productName)
                    stringBuilder.append("：")
                    stringBuilder.append(product.salesNumber)
                    stringBuilder.append("件 * ")
                    stringBuilder.append(product.salesPrice)
                    stringBuilder.append(" = ")
                    stringBuilder.append(product.totalAmount)
                }
                it.contentText = stringBuilder.toString()
            }
            .onActionLongClicked(
                "对账单" to {
                    toStatementActivity(DefaultConfig.FUNC_SALES, bean.ID, bean.totalAmount.orEmpty()) {
                        refresh()
                    }
                },
                "编辑" to {
                    if (!bean.statementList.isNullOrEmpty()) {
                        toast("已有付款记录，不可修改")
                    } else {
                        activityForResult<InventorySubActivity>(Constants.ID to bean.ID) {
                            refresh()
                        }
                    }
                },
                "销售退货" to {
                    activityForResult<InventorySubActivity>(Constants.ID to bean.ID) {
                        refresh()
                    }
                },
                "删除" to {
                    if (!bean.statementList.isNullOrEmpty()) {
                        toast("已有付款记录，不可删除")
                    } else {
                        httpGet<Any>(UrlConfig.INVENTORY_SALES_DELETE, config = {
                            isShowDialog = true
                            isSuccessToast = true
                            listParams = listOf("id" to bean.ID)
                        }) {
                            refresh()
                        }
                    }

                }
            )
            .clicked {

            }
    }


}